/******************************************************************
NOTES: 
This do file is based on the do file "universe_urban_share1_v2.do". 
It contains all the trend figures. JP (2019-9-22) ;



*******************************************************************/

# delim ;  
clear all;
set matsize 11000 ;
eststo clear ;
capture program drop add_lab_FE;
capture log close ; 
log using "../logs/Trends_figures.log", text replace;


*settings some settings that are common for the set of plots on this dofile; 

local pdf_plot_settings = 	`"
				xmtick(1980(1)2008)
				xlabel(1980(5)2010)
				ymticks(##4)
				scale(*1.5) 
				legend(on)"';
*scale(*1.3): This rescales the font size of the whole figure;	
********************************************************************;

use "../data/hpms_hwy_stats_88_08.dta", clear  ;

*---------------------------------------------------;
* - Generate variables and general cleanings ;
*---------------------------------------------------;
do "_programs_sample";
do "_setup_expenditure.do";
do "_cleaning_and_new_variables_sample.do";

gen psr_miss=cond(psr!=.,1,0);
gen iri_miss=cond(iri!=.,1,0);
drop if  surface_cat ==2 | surface_cat==3;

keep if year>1991;

tab year psr_miss, row;
tab year iri_miss , row ;

tab surface_cat, gen(Dsurface_cat);

forvalues n=1(1)3{;
	
	gen lm_surface`n'=lane_miles*Dsurface_cat`n'/1000;
};	

gen lm_resurfI2_t =lane_miles*I2_t;
gen lm_resurfIds_t=lane_miles*Ids_t ;
gen lm_resurfIall_t=lane_miles*Iall_t ;


# d ;
*Figure with resurface lane miles  for diferent dedinitions;
preserve ;

	bys year : egen total_lm=total(lane_miles);
	gen sh_lm=lane_miles/total_lm;
	gen lm_sn_d=sn_d*sh_lm;
	
	collapse (sum) lm_surface* lm_resurfIds_t lm_resurfI2_t lm_resurfIall_t lane_miles lm_sn_d, by(year) ;

	egen surf1=rowtotal(lm_surface1   );
	egen surf2=rowtotal(lm_surface1 lm_surface2   );
	egen surf3=rowtotal(lm_surface1 lm_surface2 lm_surface3  );

	gen sh_surf1=lm_surface1/(lane_miles/1000)  ;
	gen sh_surf2=lm_surface2/(lane_miles/1000) ;
	gen sh_surf3=lm_surface3/(lane_miles/1000) ;


	
	label var sh_surf3 "Flexible";
	label var sh_surf2 "Rigid";
	label var sh_surf1 "Composite";
	label var lm_sn_d "Structural number";
	save "../intermediate_data/sh_surface_type_data", replace;

restore ;		

			